﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpCart.BusinessObjects
{
    public class UserAddress: IComparable<UserAddress>
    {        
        #region Declarations
        Data.SharpDb Db;
        Data.UserAddress DataUserAddress;
        bool IsNew;
        #endregion

        #region Properties
        public int ID
        {
            get
            {
                return DataUserAddress.ID;
            }
        }
        public string Address1
        {
            get
            {
                return DataUserAddress.Address1;
            }
            set
            {
                DataUserAddress.Address1 = value;
            }
        }
        public string Address2
        {
            get
            {
                return DataUserAddress.Address2;
            }
            set
            {
                DataUserAddress.Address2 = value;
            }
        }
        public string City
        {
            get
            {
                return DataUserAddress.City;
            }
            set
            {
                DataUserAddress.City = value;
            }
        }
        public string StateRegion
        {
            get
            {
                return DataUserAddress.State_Region;
            }
            set
            {
                DataUserAddress.State_Region = value;
            }
        }
        public string ZipPostal
        {
            get
            {
                return DataUserAddress.Zip_Postal;
            }
            set
            {
                DataUserAddress.Zip_Postal = value;
            }
        }
        public string Country
        {
            get
            {
                return DataUserAddress.Country;
            }
            set
            {
                DataUserAddress.Country = value;
            }
        }
        public bool IsActive
        {
            get
            {
                return DataUserAddress.IsActive;
            }
            set
            {
                DataUserAddress.IsActive = value;
            }
        }
        public int Ordinal
        {
            get
            {
                return DataUserAddress.Ordinal;
            }
            set
            {
                DataUserAddress.Ordinal = value;
            }
        }
        #endregion

        #region Constructors
        public UserAddress()
        {
            Db = new SharpCart.Data.SharpDb();
            DataUserAddress = new SharpCart.Data.UserAddress();
            IsNew = true;
        }
        public UserAddress(int ID)
        {
            Db = new SharpCart.Data.SharpDb();
            DataUserAddress = Db.UserAddress.First(U => U.ID == ID);
            IsNew = false;
        }
        internal UserAddress(Data.SharpDb Db, Data.UserAddress DataUserAddress)
        {
            this.Db = Db;
            this.DataUserAddress = DataUserAddress;
            IsNew = false;
        }
        #endregion

        #region Instance Methods
        public void Save()
        {
            if (IsNew)
            {
                Db.AddToUserAddress(DataUserAddress);
            }
            Db.SaveChanges();
            IsNew = false;

        }

        #region IComparable<UserAddress> Members

        public int CompareTo(UserAddress other)
        {
            return this.Ordinal.CompareTo(other.Ordinal);
        }

        #endregion
        #endregion

    }
}
